﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Client.p2p
{
    /// <summary>
    /// A class that implements a command object.
    /// </summary>
    public class Command
    {
        private CommandType cmdType;
        /// <summary>
        /// [Gets/Sets]  The type of command to send.If you wanna use the Message command type,create a Message class instead of command.
        /// </summary>
        public CommandType CommandType
        {
            get { return cmdType; }
            set { cmdType = value; }
        }

        private IPEndPoint senderEP;
        /// <summary>
        /// [Gets/Sets] The IP address of command sender.
        /// </summary>
        public IPEndPoint SenderEP
        {
            get { return senderEP; }
            set { senderEP = value; }
        }

        private string senderName;
        /// <summary>
        /// [Gets/Sets] The name of command sender.
        /// </summary>
        public string SenderName
        {
            get { return senderName; }
            set { senderName = value; }
        }

        private IPEndPoint targetEP;
        /// <summary>
        /// [Gets/Sets] The target machine that will receive the command.Set this property to IPAddress.Broadcast if you want send the command to all connected clients.
        /// </summary>
        public IPEndPoint TargetEP
        {
            get { return targetEP; }
            set { targetEP = value; }
        }

/*
        private TcpClient tcpClient;
        /// <summary>
        /// [Gets] This one must be Closed manually.
        /// </summary>
        public TcpClient TcpClient
        {
            get { return tcpClient; }
        }

*/
        private byte[] commandBody;
        /// <summary>
        /// [Gets/Sets] The body of the command.This string is different in various commands.
        /// <para>Message : The text of the message.</para>
        /// <para>ClientLoginInform,ClientLogOffInform : The IP of logged in/out machine.</para>
        /// <para>***WithTimer : The interval of timer in milliseconds. The default value is 60000 equal to 1 min.</para>
        /// <para>IsNameExists : The name of client you want to check it's existence.</para>
        /// <para>Otherwise pass the "" or null.</para>
        /// </summary>
        public byte[] MetaData
        {
            get { return commandBody; }
            set { commandBody = value; }
        }

        /// <summary>
        /// Creates an instance of command object to send over the network.
        /// </summary>
        /// <param name="type">The type of command.If you wanna use the Message command type,create a Message class instead of command.</param>
        /// <param name="targetMachine">The target machine that will receive the command.Set this property to IPAddress.Broadcast if you want send the command to all connected clients.</param>
        public Command(CommandType type, IPEndPoint targetMachine)
        {
            this.cmdType = type;
            this.targetEP = targetMachine;
            this.commandBody = null;
        }

        /// <summary>
        /// Creates an instance of command object to send over the network.
        /// </summary>
        /// <param name="type">The type of command.If you wanna use the Message command type,create a Message class instead of command.</param>
        /// <param name="targetMachine">The target machine that will receive the command.Set this property to IPAddress.Broadcast if you want send the command to all connected clients.</param>
        /// <param name="metaData">
        /// The body of the command.This string is different in various commands.
        /// <para>Message : The text of the message.</para>
        /// <para>ClientLoginInform : "RemoteClientIP:RemoteClientName".</para>
        /// <para>***WithTimer : The interval of timer in milliseconds..The default value is 60000 equal to 1 min.</para>
        /// <para>IsNameExists : The name of client you want to check it's existence.</para>
        /// <para>Otherwise pass the "" or null or use the next overridden constructor.</para>
        /// </param>
        public Command(CommandType type, IPEndPoint targetMachine, byte[] metaData)
        {
            this.cmdType = type;
            this.targetEP = targetMachine;
            this.commandBody = metaData;
        }

        public Command(CommandType type, IPEndPoint senderMachine, string senderName)
        {
            this.cmdType = type;
            this.senderName = senderName;
            this.senderEP = senderMachine;
            this.commandBody = null;
        }

        public Command(CommandType type, IPEndPoint senderMachine, string senderName, byte[] metaData)
        {
            this.cmdType = type;
            this.senderName = senderName;
            this.senderEP = senderMachine;
            this.commandBody = metaData;
        }

        public Command(CommandType type, IPEndPoint senderMachine, string senderName, IPEndPoint targetMachine, byte[] metaData)
        {
            this.cmdType = type;
            this.targetEP = targetMachine;
            this.senderEP = senderMachine;
            this.senderName = senderName;
            this.commandBody = metaData;
        }

/*
        public Command(CommandType type, TcpClient tcpClient, string senderName, byte[] metaData)
        {
            this.cmdType = type;
            this.senderName = senderName;
            this.tcpClient = tcpClient;
            this.commandBody = metaData;
        }

*/
//         public Command(CommandType type, byte[] metaData)
//         {
//             this.cmdType = type;
//             this.commandBody = metaData;
//         }
    }
}
